package com.tg.message.msg;

import static com.tg.appcommon.android.TGToast.showToast;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.appbase.custom.constant.EventConstants;
import com.tange.module.statistics.TangeBizEventReport;
import com.tange.module.statistics.TangeBizStatisticsConstants;
import com.tg.appcommon.android.ClickableSpanHelper;
import com.tg.appcommon.business.IAppModule;
import com.tg.appcommon.router.TGBusiness;
import com.tg.data.http.entity.MessageDataBean;
import com.tg.message.R;

public class MessageAIView extends FrameLayout {
    private ImageView messageIcon;
    private TextView messageLabel;

    private TextView dateTv;
    private TextView timeTv;

    private TextView deviceNameTv;

    private View fullVideoView;

    private View serviceBtn;

    private TextView alertSettingTv;

    private boolean canLive = false;

    private MessageDataHolder messageDataHolder;

    public MessageAIView(Context context) {
        super(context);
        initView(context);
    }

    public MessageAIView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);

    }

    public MessageAIView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public void setMessagePlayManager(MessageDataHolder messageDataHolder) {
        this.messageDataHolder = messageDataHolder;
    }

    public void refresh() {
        MessageDataBean.ItemsBean message = messageDataHolder.getEventMessage();
        if (message == null) {
            return;
        }

        if (MessageDisplayMapping.isNoMediaEvent(message.getTag())) {
            return;
        }

        MessageDisplay messageDisplay = MessageDisplayMapping.get(message.getTag());

        if (messageDisplay != null) {
            messageIcon.setImageResource(messageDisplay.iconResId);
            messageLabel.setText(!TextUtils.isEmpty(message.getTag_msg()) ? message.getTag_msg() : getResources().getText(messageDisplay.msgLabelResId));
        } else {
            messageIcon.setImageResource(R.drawable.ic_tange_global_icon_unknown);
            messageLabel.setText(!TextUtils.isEmpty(message.getTag_msg()) ? message.getTag_msg() : getResources().getText(R.string.message_event_unknown));
        }

        deviceNameTv.setText(!TextUtils.isEmpty(message.getNickname()) ? message.getNickname() : "未知设备");

        if (!TextUtils.isEmpty(message.getStart_time())) {
            String[] dateArr = message.getStart_time().split(" ");
            if (dateArr != null && dateArr.length == 2) {
                String date = dateArr[0].replace("-", ".");
                dateTv.setText(date);
                timeTv.setText(dateArr[1]);
            }
        }

        long nowTs = System.currentTimeMillis() / 1000;
        long startTs = messageDataHolder.getEventMessage().getStart_ts();
        canLive = inAMinute(nowTs, startTs);
        TextView fullTv = findViewById(R.id.full_video_tv);
        fullTv.setText(canLive ? R.string.message_play_live : R.string.message_cloud_service_playback);

        // 上报 页面 访问 次数
        TangeBizEventReport.reportEvent(getContext(), TangeBizStatisticsConstants.Super_AI_Page_View_Count);
    }

    public static boolean inAMinute(long endTimestamp, long startTimestamp) {
        return endTimestamp - startTimestamp <= 60;
    }

    private void initView(Context context) {

        LayoutInflater.from(context).inflate(R.layout.layout_message_super_ai, this);

        TextView topTitle = findViewById(R.id.top_title);
        topTitle.setText(Html.fromHtml(getResources().getString(R.string.present_by_super_ai_push)));

        messageIcon = findViewById(R.id.message_icon);
        messageLabel = findViewById(R.id.message_label);
        deviceNameTv = findViewById(R.id.tv_device_name);
        dateTv = findViewById(R.id.tv_date);
        timeTv = findViewById(R.id.tv_time);
        fullVideoView = findViewById(R.id.line_full_video);
        serviceBtn = findViewById(R.id.buy);
        alertSettingTv = findViewById(R.id.tv_alert_setting);

        serviceBtn.setOnClickListener(v -> {
            IAppModule iAppModule = TGBusiness.getAppModule();
            iAppModule.launchSuperAIPay(getContext(), messageDataHolder.getDeviceItem());
            TangeBizEventReport.reportEvent(getContext(), TangeBizStatisticsConstants.Super_AI_Page_Pay_Click_Count);
        });

        fullVideoView.setOnClickListener(v -> onClickFull());

        alertSettingTv.setText(ClickableSpanHelper.getClickableSpanForMessage(alertSettingTv.getText().toString(), v -> {
            if (messageDataHolder.getDeviceItem() != null){
                if (messageDataHolder.getEventMessage() == null) return;
                if (TextUtils.equals(messageDataHolder.getEventMessage().getTag(), EventConstants.LOW_TEMP)
                        || TextUtils.equals(messageDataHolder.getEventMessage().getTag(), EventConstants.HIGH_TEMP)
                        || TextUtils.equals(messageDataHolder.getEventMessage().getTag(), EventConstants.LOW_HUMID)
                        || TextUtils.equals(messageDataHolder.getEventMessage().getTag(), EventConstants.HIGH_HUMID)) {
                }else{}

            } else {
                showToast(R.string.network_error);
            }
        }));
        alertSettingTv.setMovementMethod(LinkMovementMethod.getInstance());

    }

    private void onClickFull() {
        if (messageDataHolder.getEventMessage() == null) return;
        if (messageDataHolder.getEventMessage() != null && !TextUtils.isEmpty(messageDataHolder.getEventMessage().getUrl())) {
            if (TextUtils.equals(messageDataHolder.getEventMessage().getMessage_type(), EventConstants.NOTICE)) {
                IAppModule iAppModule = TGBusiness.getAppModule();
                iAppModule.openServePlay((Activity) getContext(), messageDataHolder.getEventMessage().getUrl(), Long.parseLong(messageDataHolder.getEventMessage().getDevice_id()), 0, Activity.RESULT_FIRST_USER);
            }
        } else {
            if (messageDataHolder.getDeviceItem() == null) {
                showToast(R.string.message_device_no_found);
                return;
            }

            IAppModule iAppModule = TGBusiness.getAppModule();
            iAppModule.onClickServeBtn((Activity) getContext(), canLive, messageDataHolder.getDeviceItem(), messageDataHolder.getEventMessage());
        }
    }
}
